package com.example.airobotinterviewrecord.task;

import com.example.airobotinterviewrecord.entity.InterviewSchedule;
import com.example.airobotinterviewrecord.service.IInterviewScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;

@Component
public class InterviewReminderTask {

    @Autowired
    private IInterviewScheduleService interviewScheduleService;

    /**
     * 每 5 分钟执行一次定时任务
     */
    @Scheduled(fixedRate = 5 * 60 * 1000)
    public void remindInterviews() {
        List<InterviewSchedule> schedules = interviewScheduleService.listSchedulesByPage(1, Integer.MAX_VALUE).getRecords();
        LocalDateTime now = LocalDateTime.now();

        for (InterviewSchedule schedule : schedules) {
            LocalDateTime interviewTime = schedule.getInterviewTime();
            if (interviewTime != null) {
                long hours = ChronoUnit.HOURS.between(now, interviewTime);
                if (hours == 1) {
                    System.out.println("距离候选人 " + schedule.getCandidateId() + " 的面试还剩 1 小时，请准备！");
                }
            }
        }
    }
}